"use strict";
const express = require('express');
const router = express.Router();
const CONSTANT =  require("../const/CONST");
const productsVideosUtil = require("../utils/routerUtils/productsVideosUtil");
const retUtil = require('../utils/commonUtils/retUtil');
const tokenCheck = require('../middleware/tokenCheck');
router.use(tokenCheck);

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
});



/***
 * 创建视频作品
 * @author mk
 * @date 2018-5-30
 * 参数：token      必须   入驻机构唯一标识
 * 参数：design_id  必须   视频模板唯一标识
 * 参数：content    必须   视频模板对应的内容结构, 的 json 字符串
 * 测试地址 : http://localhost:3014/v1/products/videos/crateVideoDoc?token=XXXXX&design_id=90&content=[{"id":1,"type":1,"max":12,"text":"哈哈哈哈"}]
 */
function crateVideoDoc(req, res, next) {
    let token = retUtil.stringFromReq(req, 'token');
    let designId = retUtil.numberFromReq(req,'design_id');
    let content = retUtil.stringFromReq(req,'content');
    //初步判断参数的有效性
    if (!token || !designId  || !content || token.length <= 0 || content.length <= 0 ||  designId <= 0){
        return retUtil.ret_err_json(CONSTANT.CODE.INVALID_PARAMS,res);
    }
    //创建视频作品
    productsVideosUtil.crateVideoDoc(token,designId,content,function (err,data) {
        if (err){
            console.log("routes/v1/products/videos.js  crateVideoDoc err" + err);
            return retUtil.ret_err_json(err,res);
        }
        return retUtil.ret_succ_json(data,res);
    });

}
router.post('/crateVideoDoc', crateVideoDoc);
router.get('/crateVideoDoc', crateVideoDoc);


/***
 * 修改视频作品
 * @author mk
 * @date 2018-5-30
 * 参数：token       必须   入驻机构唯一标识
 * 参数：product_id  必须   视频模板唯一标识
 * 参数：content     必须   视频模板对应的内容结构, 的 json 字符串
 * 测试地址 : http://localhost:3014/v1/products/videos/updateVideoDoc?token=XXXXX&product_id=90&content=[{"id":1,"type":1,"max":12,"text":"哈哈哈哈"}]
 */
function updateVideoDoc(req, res, next) {
    let token = retUtil.stringFromReq(req, 'token');
    let productId = retUtil.numberFromReq(req,'product_id');
    let content = retUtil.stringFromReq(req,'content');
    //初步判断参数的有效性
    if (!token || !productId  || !content || token.length <= 0 || content.length <= 0 ||  productId <= 0){
        return retUtil.ret_err_json(CONSTANT.CODE.INVALID_PARAMS,res);
    }
    //修改视频作品
    productsVideosUtil.updateVideoDoc(token,productId,content,function (err,data) {
        if (err){
            console.log("routes/v1/products/videos.js  updateVideoDoc err" + err);
            return retUtil.ret_err_json(err,res);
        }
        return retUtil.ret_succ_json(data,res);
    });
}
router.post('/updateVideoDoc', updateVideoDoc);
router.get('/updateVideoDoc', updateVideoDoc);


/***
 * 获取视频作品信息
 * @author mk
 * @date 2018-5-30
 * 参数：token        必须   入驻机构唯一标识
 * 参数：product_id   必须   视频模板唯一标识
 * 测试地址 : http://localhost:3014/v1/products/videos/getVideoDocConetnt?token=XXXXX&product_id=90
 */
function getVideoDocContent(req, res, next) {
    let token = retUtil.stringFromReq(req, 'token');
    let productId = retUtil.numberFromReq(req,'product_id');
    //初步判断参数的有效性
    if (!token || !productId  ||  token.length <= 0 || productId <= 0){
        return retUtil.ret_err_json(CONSTANT.CODE.INVALID_PARAMS,res);
    }
    //获取视频作品信息
    productsVideosUtil.getVideoDocContent(token,productId,function (err,data) {
        if (err){
            console.log("routes/v1/products/videos.js  getVideoDocContent err" + err);
            return retUtil.ret_err_json(err,res);
        }
        return retUtil.ret_succ_json(data,res);
    });
}
router.post('/getVideoDocContent', getVideoDocContent);
router.get('/getVideoDocContent', getVideoDocContent);

/***
 * 获取视频作品信息
 * @author mk
 * @date 2018-5-30
 * 参数：token        必须   入驻机构唯一标识
 * 参数：product_id   必须   视频模板唯一标识
 * 测试地址 : http://localhost:3014/v1/products/videos/typesetVideodoc?token=XXXXX&product_id=90
 */
function typesetVideoDoc(req, res, next) {
    let token = retUtil.stringFromReq(req, 'token');
    let productId = retUtil.numberFromReq(req,'product_id');
    //初步判断参数的有效性
    if (!token || !productId  ||  token.length <= 0 || productId <= 0){
        return retUtil.ret_err_json(CONSTANT.CODE.INVALID_PARAMS,res);
    }
    //获取视频作品信息
    productsVideosUtil.typesetVideoDoc(token,productId,function (err,data) {
        if (err){
            console.log("routes/v1/products/videos.js  typesetVideoDoc err" + err);
            return retUtil.ret_err_json(err,res);
        }
        return retUtil.ret_succ_json(data,res);
    });
}
router.post('/typesetVideoDoc', typesetVideoDoc);
router.get('/typesetVideoDoc', typesetVideoDoc);

module.exports = router;
